//
//  findpasswordtextViewController.m
//  BAOASystem
//
//  Created by michael on 2018/2/7.
//  Copyright © 2018年 czd. All rights reserved.
//
#import "OALoginViewController.h"
#import "findpasswordtextViewController.h"
#import "findpasswordemailViewController.h"
static int countDownTime = 180;

@interface findpasswordtextViewController ()

@end

@implementation findpasswordtextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
      self.navigationController.navigationBar.hidden = NO;

     NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:

[UIColor whiteColor],NSForegroundColorAttributeName, nil];
 
[self.navigationController.navigationBar setTitleTextAttributes:attributes];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"0a62ea"];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        self.title = NSLocalizedString(@"找回密码", nil);
    self.view.backgroundColor = [UIColor colorWithHexString:@"f0eff5"];
        [self initview];
}
- (void)initview{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, KScreenWidth-20, 20)];
label.textColor = [UIColor colorWithHexString:@"484848"];
  NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];

label.text = [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"您的登陆名:", nil),name];
label.backgroundColor = [UIColor clearColor];
[self.view addSubview:label];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(10, label.bottom+10, KScreenWidth-20, 20)];
label2.textColor = [UIColor colorWithHexString:@"484848"];
label2.text =NSLocalizedString(@"请输入与您登录名相匹配的手机号码", nil);
label2.backgroundColor = [UIColor clearColor];
[self.view addSubview:label2];
_textfield = [[UITextField alloc]initWithFrame:CGRectMake(10, label2.bottom+10, KScreenWidth-150, 40)];
     _textfield.delegate = self;

//  [_tex182tfield addTarget:self action:@selector(endediting) forControlEvents:UIControlEventEditingDidEnd];
  // 可在自定义selector处
_textfield.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_textfield];

  _yanzbutton = [myView creatButtonWithFrame:CGRectMake(_textfield.right+10, _textfield.top,120,40 ) title:NSLocalizedString(@"获取验证码", nil) tag:1000 tintColor:[UIColor whiteColor] backgroundColor:[UIColor colorWithHexString:@"0aa0f6"]];
  _yanzbutton.titleLabel.font    = [UIFont systemFontOfSize: 14];

//        _yanzbutton.layer.cornerRadius = 10;
        [_yanzbutton addTarget:self action:@selector(getVerificationCode) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_yanzbutton];
    
        UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(10, _textfield.bottom+10, KScreenWidth-20, 20)];
label3.textColor = [UIColor colorWithHexString:@"484848"];
label3.text =NSLocalizedString(@"短信验证码", nil);
label3.backgroundColor = [UIColor clearColor];
[self.view addSubview:label3];
_textfield2 = [[UITextField alloc]initWithFrame:CGRectMake(10, label3.bottom+10, KScreenWidth-20, 40)];

_textfield2.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_textfield2];

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
      button2.frame = CGRectMake(10, _textfield2.bottom+10, KScreenWidth-20, 40);
          button2.backgroundColor = [UIColor colorWithHexString:@"0a62ea"];

    [button2 setTitle:NSLocalizedString(@"确认", nil) forState:UIControlStateNormal];
    button2.titleLabel.font = [UIFont systemFontOfSize: 14.0];
          [button2 setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
//        button2.layer.cornerRadius = button.height/2;
                [button2 addTarget:self action:@selector(action2) forControlEvents:UIControlEventTouchUpInside];

            [self.view addSubview:button2];
 UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
      button3.frame = CGRectMake(10, button2.bottom+10, KScreenWidth-20, 40);
//          button3.backgroundColor = [UIColor colorWithHexString:@"0a62ea"];

button3.backgroundColor = [UIColor clearColor];
    [button3 setTitle:NSLocalizedString(@"跳转短信找回", nil) forState:UIControlStateNormal];
    button3.titleLabel.font = [UIFont systemFontOfSize: 14.0];
          [button3 setTitleColor:[UIColor colorWithHexString:@"0aa0f6"]forState:UIControlStateNormal];
                [button3 addTarget:self action:@selector(action3) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:button3];

}
-(void)endediting
{
NSString *oldstring=[_textfield.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

if ([oldstring isEqualToString:@""]|| oldstring.length == 0) {
     [self.view makeToast:NSLocalizedString(@"请输入与您登录名相匹配的手机号码", nil)];
}else{

NSString *phonestring=[_textfield.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
     NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
     NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];

NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>administrator<userName><password>b628e2c3b78ef9bf269c489b35176e48</password><website>%@</website><language>%@</language><type>%@</type></header><body><user><username>%@</username><phone>%@</phone></user></body></message>",website,language,BAtype,name,oldstring];
    bodyStr= [bodyStr URLEncodedString];

 NSArray * array = @[@{@"arg0":bodyStr}];
 
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bcheckPhone];

 [HttpTools requestWithURL:path arrays:array method:@"checkPhone" success:^(id JSON) {
    [MBProgressHUD hideHUDForView:self.view animated:YES];
NSLog(@"%@",JSON);
[self.view makeToast:JSON[@"msg"]];
if ([[NSString stringWithFormat:@"%@",JSON[@"code"]]isEqualToString:@"200"])
{
 
_type = @"ok";
}
    } failure:^(NSError *error) {
      [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];

}
}

- (void)getVerificationCode
{
NSString *oldstring=[_textfield.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

if ([oldstring isEqualToString:@""]|| oldstring.length == 0) {
     [self.view makeToast:NSLocalizedString(@"请输入与您登录名相匹配的手机号码", nil)];
}
//else if (![_type isEqualToString:@"ok"]) {
//        [self.view makeToast:NSLocalizedString(@"手机号码不存在", nil)];
//}

else{



    _label = [[UILabel alloc]initWithFrame:self.yanzbutton.bounds];
    
    _timer = [[NSTimer alloc]init];
    _timer= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    [_timer setFireDate:[NSDate distantPast]];
    //请求服务器发送短信验证码
    //......
      NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];

      NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
   NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>administrator</userName><password>b628e2c3b78ef9bf269c489b35176e48</password><website>%@</website><language>%@</language><type>%@</type></header><body><user><username>%@</username><phone>%@</phone></user></body></message>",website,language,BAtype,name,oldstring];
       bodyStr= [bodyStr URLEncodedString];

 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bSendSms];

 [HttpTools requestWithURL:path arrays:array method:@"SendSms" success:^(id JSON) {
    [MBProgressHUD hideHUDForView:self.view animated:YES];
NSLog(@"%@",JSON);
[self.view makeToast:JSON[@"msg"]];
if ([[NSString stringWithFormat:@"%@",JSON[@"code"]]isEqualToString:@"200"])
{
 
_codeId = JSON[@"codeId"];}
    } failure:^(NSError *error) {
      [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}
}
- (void)countDown
{
    countDownTime--;
    if (countDownTime<=0||countDownTime ==180) {
        _label.hidden = YES;
        _yanzbutton.hidden = NO;
        _yanzbutton.userInteractionEnabled = YES;
        [_timer setFireDate:[NSDate distantFuture]];
        countDownTime =180;
    }else{

        _label.hidden = NO;
        _label.text = [NSString stringWithFormat:@"(%ds)%@",countDownTime,NSLocalizedString(@"重新获取", nil)];
        _label.font = [UIFont systemFontOfSize:12];
        _label.backgroundColor = [UIColor colorWithRed:18/255.0  green:129/255.0  blue:201/255.0 alpha:1];
        _label.layer.cornerRadius = 8;
        _label.clipsToBounds = YES;
        _label.textAlignment = NSTextAlignmentCenter;
        _label.textColor= [UIColor whiteColor];
        [_yanzbutton addSubview:_label];
        _yanzbutton.userInteractionEnabled = NO;
    }
}
- (void)action2{
NSString *yanzhengstring=[_textfield2.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *oldstring=[_textfield.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([oldstring isEqualToString:@""]|| oldstring.length == 0) {
     [self.view makeToast:NSLocalizedString(@"请输入与您登录名相匹配的手机号码", nil)];
}
else if ([yanzhengstring isEqualToString:@""]|| yanzhengstring.length == 0) {
     [self.view makeToast:NSLocalizedString(@"请输入验证码", nil)];
}else{

 NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];

      NSString *website = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
   NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
   NSString *bodyStr = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><message><header><userName>administrator</userName><password>b628e2c3b78ef9bf269c489b35176e48</password><website>%@</website><language>%@</language><type>%@</type></header><body><username>%@</username><phone>%@</phone><code>%@</code><codeId>%@</codeId></body></message>",website,language,BAtype,name,oldstring,yanzhengstring,_codeId];
       bodyStr= [bodyStr URLEncodedString];

 NSArray * array = @[@{@"arg0":bodyStr}];
     NSString * path = [NSString stringWithFormat:@"%@%@",BAip,bResetPassword];

 [HttpTools requestWithURL:path arrays:array method:@"ResetPassword" success:^(id JSON) {
    [MBProgressHUD hideHUDForView:self.view animated:YES];
NSLog(@"%@",JSON);
[self.view makeToast:JSON[@"msg"]];
if ([[NSString stringWithFormat:@"%@",JSON[@"code"]]isEqualToString:@"200"])
{
 
 UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

        OALoginViewController * loginVC = [storyBoard instantiateViewControllerWithIdentifier:@"LoginVC"];
        UINavigationController * navVC = [[UINavigationController alloc]initWithRootViewController:loginVC];
    self.view.window.rootViewController = navVC;
        loginVC.navigationController.navigationBar.hidden = YES;}
    } failure:^(NSError *error) {
      [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];


}


}
- (void)action3{

findpasswordemailViewController *vc = [[findpasswordemailViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
